import { _decorator, Color, Component, Material, MeshRenderer, Node, sp } from 'cc';
const { ccclass, property } = _decorator;
const SPEED = 2000

@ccclass('Role')
export class Role extends Component {

    private _roleSke: sp.Skeleton;
    private currentColorValue: number[] = [];
    private _mat: Material | null = null;
    private _startShowAni: boolean = false;
    private _timer: number = 0;
    private _glowState: 'stay' | 'start' = 'stay';
    private _selectFlag: Node;
    private _newFlag: Node;
    private _callback: Function;
    private _thisOrg: any;
    protected onLoad(): void {
        this._selectFlag = this.node.getChildByName('select');
        this._newFlag = this.node.getChildByName('NEW');
        this.initMat();
    }
    start() {

    }

    setActive(active: boolean) {
        this.node.active = active;
    }
    private initMat() {
        this._startShowAni = false;
        this._roleSke = this.node.getChildByName('Node').getComponent(sp.Skeleton);
        if (!this._roleSke) return;
        if (!this._roleSke.customMaterial) return;
        this._mat = new Material();
        // const color = this._roleSke.customMaterial.getProperty('glowColor') as Color;
        // this.currentColorValue = [color.r, color.g, color.b];
        this._mat.copy(this._roleSke.customMaterial);
        this._mat.setProperty('glowColor', Color.BLACK);
        this._roleSke.customMaterial = this._mat;
    }
    public startGlow(cb?: Function, thisobj?: any) {
        this.node.active = true;
        this._callback = cb;
        this._thisOrg = thisobj;
        this._timer = 0;
        this._glowState = 'stay';
        this._startShowAni = true;
        this._newFlag.active = true;
    }

    public setSelect() {
        this._selectFlag.active = true;
    }

    public setGray() {
        this._roleSke.color = new Color(37, 37, 37, 255);
    }

    update(deltaTime: number) {
        //  this.playShowGlow(deltaTime);
    }

    private playShowGlow(deltaTime: number) {
        if (!this._roleSke.customMaterial) return;
        if (!this._startShowAni) return;
        if (!this._mat) return;
        if (this.currentColorValue.reduce((a, b) => a + b) === 0) {
            this._startShowAni = false;
            this._callback?.call(this._thisOrg);
            return;
        }

        this._timer += deltaTime;

        if (this._timer > 0.5) {
            this._glowState = 'start';
        } else {
            this._glowState = 'stay';
        }

        if (this._glowState === 'start') {

            // 对RGB三个通道分别递减
            this.currentColorValue = [
                Math.max(0, this.currentColorValue[0] - deltaTime * SPEED),
                Math.max(0, this.currentColorValue[1] - deltaTime * SPEED),
                Math.max(0, this.currentColorValue[2] - deltaTime * SPEED)
            ];
            // 转换为整数
            const colorValue = [
                Math.floor(this.currentColorValue[0]),
                Math.floor(this.currentColorValue[1]),
                Math.floor(this.currentColorValue[2])
            ];
            //  设置颜色

            this._mat.setProperty('glowColor', new Color(colorValue[0], colorValue[1], colorValue[2], 255));
            this._roleSke.customMaterial = this._mat;
        }
    }
}


